// 根据用户角色得到对应的路由

const toRoleRoutes = {
    // 'coustomer': ['Goods', 'GoodsList', 'GoodsAdd', 'GoodsEdit'],
    // 'admin': ['Goods', 'GoodsList', 'GoodsAdd', 'Category', 'GoodsEdit']
    'coustomer': [{
        name: "Goods"
    }, {
        name: "GoodsList"
    }, {
        name: "GoodsAdd"
    }, {
        name: "GoodsEdit"
    },],
    'admin': [{
        name: "Goods"
    }, {
        name: "GoodsList"
    }, {
        name: "GoodsAdd"
    }, {
        name: "Category"
    }, {
        name: "GoodsEdit"
    },]

}

export default function getRole(role, routes) {
    // let roleToRoutes = toRoleRoutes[role];
    // let res = routes.filter((r) => {
    //     if (roleToRoutes.indexOf(r.name) !== -1) {
    //         r.children = r.children.filter((r) => {
    //             return roleToRoutes.indexOf(r.name) !== -1
    //         })
    //         return true;
    //     }
    // })
    // return res;
    const allowRoutes = toRoleRoutes[role].map((item) => item.name);
    const result = routes.filter((r) => {
        if (allowRoutes.indexOf(r.name) !== -1) {
            const children = r.children;
            r.children = children.filter((r) => allowRoutes.indexOf(r.name) !== -1);
            return true;
        }
        return false;
    })
    return result;
}